package com.xuef.ibatis.session;

import com.xuef.ibatis.builder.xml.XMLConfigBuilder;
import com.xuef.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by xuefengDong: moveb
 * On 2019/4/26- 21:12
 */
public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {

        return null;
    }

    public SqlSessionFactory build(InputStream inputStream) throws Exception {
        try {
            XMLConfigBuilder parser = new XMLConfigBuilder(inputStream);
            return build(parser.parse());
        } catch (Exception e) {
            //TODO
            throw new Exception("Error building SqlSession.", e);
        } finally {
            //ErrorContext.instance().reset();
            try {
                inputStream.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
    }
}
